package com.blackline.auto.Report;

import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author Joie
 * @version Aug 19, 2013
 *          <p>
 *          This is run a single UPS report with specify setting.
 *          Test Env is T2/Test2
 */


public class RunSingleUPSReport
{

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger = Logger.getLogger(RunSingleUPSReport.class);
	UI UI = new UI();
	Parameters parameters;

	int sleepTime = 2000;

    //Log in User as System Admin
	TestData testdata = new TestData("TestData/RunStdReportsWithAllRoles.xml");

	String t2Test2 = testdata.getInput("t2Test2");
	String user = testdata.getInput("user");
	String password = testdata.getInput("password");
	
	//Get all setting test data
	TestData singleUPSData = new TestData("TestData/Setting_SingleUPSReport.xml");
	String role = singleUPSData.getInput("Role");
	String rptName = singleUPSData.getInput("RptName");
	String format = singleUPSData.getInput("Format");
	String group = singleUPSData.getInput("Group");
	String period = singleUPSData.getInput("Period");
	String entity = singleUPSData.getInput("Entity");
	String team = singleUPSData.getInput("Team");
	String itemClass = singleUPSData.getInput("ItemClass");

	@Before
	public void setUp() throws Exception
	{
		FirefoxProfile profile = new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters = new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}

	
	
	@Test
	public void RunSingle_UPSReport() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + t2Test2 + " with: " + user);
		UI.LogonPage.Login(driver, t2Test2, user, password);
		
		
		// Start run the report with different setting
		runUPSReportsWithSpecificSetting(role,t2Test2,rptName,format,group,period,entity,team,itemClass);
		
	}

	
	@After
	public void tearDown() throws Exception
	{
		 CommonMethods.takeScreenShot(this.getClass(), driver);
		 logger.info("===========================================");
		driver.close();
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	/**
	 * This is to run the UPS reports with specific roles
	 * 
	 * @param roleName
	 *            The name of the role to run the reports
	 */
	private void runUPSReportsWithSpecificSetting(String role, String site, String rptName, String format, String group, String peroid, String entity, String team, String itemclass) throws Exception
	{
		// Switch to different roles
		UI.SessionMenu.switchRole(role);

		// Go to Enhanced Reporting page
		Thread.sleep(sleepTime);
		driver.get(site);
		Thread.sleep(sleepTime);

		// ======== Go to Accounts node========
		logger.info("=====UPS Node=====");
		UI.Reporting.UPS_Reports_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		UI.Reporting.AddIcon_UPSReports.click();
		Thread.sleep(sleepTime);
		UI.Reporting.UPS_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		
		//Select report which need run
		UI.Reporting.Name_text_field.clear();
		Thread.sleep(sleepTime);
		UI.Reporting.Name_text_field.sendKeys(rptName);
		Thread.sleep(sleepTime);
		UI.Reporting.Refresh_button.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Run_linkOfSearchedResult.click();
		Thread.sleep(sleepTime);
		logger.info("=====UPS Report : " + rptName + " has been selected to run=====");
		
		//Switch the format as required
		switch (format)
		{
		case "Excel": UI.RunPrompts.Excel_Format_Dropdownlist.click(); break;
		case "PDF": UI.RunPrompts.PDF_Format_Dropdownlist.click(); break;
		case "CSV": UI.RunPrompts.CSV_Format_Dropdownlist.click(); break;
		default: UI.RunPrompts.Web_Format_Dropdownlist.click(); break;
		}
		Thread.sleep(sleepTime);
		logger.info("[Pass!] Report format : " + format + " has been selected.");
		
		
		//Switch the group as required
		switch (group)
		{
		case "GL": UI.RunPrompts.GLAcct_Group_Dropdownlist.click(); break;
		case "GLGroup": UI.RunPrompts.GLGroupAcct_Group_Dropdownlist.click(); break;
		case "Group": UI.RunPrompts.GroupAcct_Group_Dropdownlist.click(); break;
		case "Management": UI.RunPrompts.Management_Group_Dropdownlist.click(); break;
		default: UI.RunPrompts.Recon_Group_Dropdownlist.click(); break;
		}
		Thread.sleep(sleepTime);
		logger.info("[Pass!] Run report group : " + group + " has been selected.");
		
		//Select required period.
		UI.RunPrompts.selectDate(driver, peroid).click();
		Thread.sleep(sleepTime);
		logger.info("[Pass!] Run report peroid : " + peroid + " has been selected.");
		
		//Select required Class Item
		switch (itemclass)
		{
		case "Timing": UI.RunPrompts.Timing_ClassItem_Dropdownlist.click(); break;
		case "List Component": UI.RunPrompts.ListComponent_ClassItem_Dropdownlist.click(); break;
		case "Required Adjustment": UI.RunPrompts.RequiredAdjustment_ClassItem_Dropdownlist.click(); break;
		default: UI.RunPrompts.All_ClassItem_Dropdownlist.click(); break;
		}
		Thread.sleep(sleepTime);
		logger.info("[Pass!] Run report item class : " + itemclass + " has been selected.");
		
		if(UI.RunPrompts.Team_ChangeView.getAttribute("title").equalsIgnoreCase("Switch To Tree View"))
		{
			UI.RunPrompts.Team_InputText.clear();
			Thread.sleep(sleepTime);
		    UI.RunPrompts.Team_InputText.sendKeys(team);
		    Thread.sleep(sleepTime);
		}	    
	    else
	    {
	         UI.RunPrompts.Team_ChangeView.click();
	         Thread.sleep(sleepTime);
	         UI.RunPrompts.Entity_InputText.clear();
	         Thread.sleep(sleepTime);
	         UI.RunPrompts.Entity_InputText.sendKeys(team);
	         Thread.sleep(sleepTime);
	    }
		
		logger.info("[Pass!] Run report Team : " + team + " has been selected.");
		
		
		if(UI.RunPrompts.Entity_ChangeView.getAttribute("title").equalsIgnoreCase("Switch To Combo View"))
			{
			UI.RunPrompts.Entity_ChangeView.click();
		    Thread.sleep(sleepTime);
		    UI.RunPrompts.Entity_InputText.clear();
		    Thread.sleep(sleepTime);
		    UI.RunPrompts.Entity_InputText.sendKeys(entity);
		    Thread.sleep(sleepTime);
			}	    
		else
		{
			UI.RunPrompts.Entity_InputText.clear();
		    Thread.sleep(sleepTime);
			UI.RunPrompts.Entity_InputText.sendKeys(entity);
			Thread.sleep(sleepTime);
		}
		
		logger.info("[Pass!] Run report Entity : " + entity + " has been selected.");
		
		UI.RunPrompts.Run_Report_button.click();
		Thread.sleep(sleepTime);
		
		if(UI.Alert_inReporting.Message_Text.getText().endsWith("This report is being processed and will be available in the Report Log."))
			{
			UI.Alert_inReporting.OK_button.click();
			Thread.sleep(sleepTime);
		    logger.info("[Pass!] Run report succeed.");
			}
		else
		{
			logger.info("[Failed!] Run report Failed.");
		}

	}


}

